package com.ruoyi.web.controller.system;

import java.util.List;

import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.framework.config.ServerConfig;
import com.ruoyi.system.domain.SysCheck;
import com.ruoyi.system.domain.SysContractMoney;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SysPurchaseContract;
import com.ruoyi.system.service.ISysPurchaseContractService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 【合同签订及预付款支付】Controller
 * 
 * @author zhaoguanyu
 * @date 2022-01-20
 */
@Api(tags = "合同签订及预付款支付")
@RestController
@RequestMapping("/system/contract")
public class SysPurchaseContractController extends BaseController
{
    @Autowired
    private ISysPurchaseContractService sysPurchaseContractService;
    @Autowired
    private ServerConfig serverConfig;


    /**
     * 获取采购合同审批
     */
    @PreAuthorize("@ss.hasPermi('lifecycle:contract:query')")
    @GetMapping(value = "/{itemNo}")
    public AjaxResult getInfo(@PathVariable("itemNo") String itemNo)
    {
        return AjaxResult.success(sysPurchaseContractService.selectSysPurchaseContractById(itemNo));
    }


    /**
     * 新增【插入合同签订】
     */
    @PreAuthorize("@ss.hasPermi('lifecycle:contract:add')")
    @Log(title = "【插入合同签订和预付款支付】", businessType = BusinessType.INSERT)
    @PostMapping("/insert")
    public AjaxResult addmonty(@RequestBody SysContractMoney sysContractMoney)
    {
        return toAjax(sysPurchaseContractService.insertSysPurchaseContract(sysContractMoney));
    }

    /**
     *提交项目合同
     * @Author zhaoguanyu
     * @Date 13:59 2021/12/21
     */
    @ApiOperation(value = "提交验收报告")
    @PreAuthorize("@ss.hasPermi('lifecycle:contract:submit')")
    @Log(title = "验收阶段", businessType = BusinessType.IMPORT)
    @PostMapping("/upload")
    public AjaxResult uploadFile(MultipartFile file) throws Exception
    {
        try
        {
            // 上传文件路径
            String filePath = RuoYiConfig.getUploadPath();
            // 上传并返回新文件名称
            String fileName = FileUploadUtils.upload(filePath, file);
            String url = serverConfig.getUrl() + fileName;
            AjaxResult ajax = AjaxResult.success();
            //文件中文名称
            String name = file.getOriginalFilename();
            SysContractMoney sysContractMoney = new SysContractMoney();
            sysContractMoney.setFileName(name);
            sysContractMoney.setUrl(url);
            sysContractMoney.setOnlyName(fileName);
            Long id = sysPurchaseContractService.insertFileUpload(sysContractMoney);
            ajax.put("id",id);
            ajax.put("fileName", fileName);
            ajax.put("url", url);
            return ajax;
        }
        catch (Exception e)
        {
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 新增【预付款支付】
     */
    @PreAuthorize("@ss.hasPermi('lifecycle:contract:add')")
    @Log(title = "【预付款支付】", businessType = BusinessType.INSERT)
    @PostMapping("/update")
    public AjaxResult add(@RequestBody SysContractMoney sysContractMoney)
    {
        return toAjax(sysPurchaseContractService.updateSysPurchaseContract(sysContractMoney));
    }

    /**
     *提交支付凭证(图片)
     * @Author zhaoguanyu
     * @Date 13:59 2021/1/20
     */
    @ApiOperation(value = "提交支付凭证")
    @PreAuthorize("@ss.hasPermi('lifecycle:contract:submit')")
    @Log(title = "验收阶段", businessType = BusinessType.IMPORT)
    @PostMapping("/pupload")
    public AjaxResult uploadPFile(MultipartFile file) throws Exception
    {
        try
        {
            // 上传文件路径
            String filePath = RuoYiConfig.getUploadPath();
            // 上传并返回新文件名称
            String p_fileName = FileUploadUtils.upload(filePath, file);
            String p_url = serverConfig.getUrl() + p_fileName;
            AjaxResult ajax = AjaxResult.success();
            //文件中文名称
            String name = file.getOriginalFilename();
            SysContractMoney sysContractMoney = new SysContractMoney();
            sysContractMoney.setpFileName(name);
            sysContractMoney.setpUrl(p_url);
            sysContractMoney.setpOnlyName(p_fileName);
            Long id = sysPurchaseContractService.uploadPFile(sysContractMoney);
            ajax.put("p_id",id);
            ajax.put("p_fileName", p_fileName);
            ajax.put("p_url", p_url);
            return ajax;
        }
        catch (Exception e)
        {
            return AjaxResult.error(e.getMessage());
        }
    }
}
